#!/usr/bin/env bash 
cd $(dirname $0)
main() {
    echo 
    comp 
}

#-------------------------
# 编译并运行
#-------------------------
function comp() {
    local src=$(find -type f -regex ".*\.\(c\|cpp\)")
    local lstr=""
    for i in $src 
    do 
        g++ -std=c++11 -c $i 
        if [[ $i == *".cpp" ]] ; then
            lstr="$lstr $(basename $i .cpp).o"
        elif [[ $i == *".c" ]] ; then
            lstr="$lstr $(basename $i .c).o"
        fi 
    done 
    # echo $lstr 
    local sys=$(CheckSys)
    g++ -std=c++11 $lstr -o mexe
    if [[ $sys == "cygwin" ]] ; then 
        echo "Platform: cygwin"
        mv mexe.exe debug/mexe.exe 
        chmod +x debug/mexe.exe 
        ./debug/mexe.exe
        rm -f *.o 
        return 0
    fi 
    if [[ $sys == "linux" ]] ; then 
        echo "Platform: linux"
        mv mexe debug/mexe 
        chmod +x debug/mexe 
        ./debug/mexe
        rm -f *.o 
        return 0
    fi 
}

#----------------------
#系统检测
#----------------------
function CheckSys() {
    local sys=$(uname -s | tr '[A-Z]' '[a-z]')
    case $sys in 
        *cygwin*) echo "cygwin" ;;
        *linux*) echo "linux"  ;;
        *) echo "unknow" ;; 
    esac 
}

main $*  